﻿///***********************************************************************
/// DataAccessBase.cs
/// Owner: joemu
///
/// Provides a base class for the .NET Stored Procedure Wrapper classes.
///
/// Copyright(c) Microsoft Corporation
///***********************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;

namespace OR.Reuse.Persistence
{
    /// <summary>
    /// Provides a base class for the .NET Stored Procedure Wrapper classes.
    /// </summary>
    public class DataAccessBase
    {
        private static string mConnectionString;

        private SqlConnection mConnection;
        private bool mMaintainConnection;

        public static string ConnectionString
        {
            get { return mConnectionString; }
            set { mConnectionString = value; }
        }

        /// <summary>
        /// Gets or sets the SqlConnection that the data access class will use to
        /// execute SqlCommands.
        /// </summary>
        public SqlConnection Connection
        {
            get { return mConnection; }
            set { mConnection = value; }
        }

        /// <summary>
        /// Gets or sets whether to maintain an open SqlConnection across command
        /// executions.  If this value is false, the SqlConnection should be closed
        /// at the conclusion of each data access method.
        /// </summary>
        public bool MaintainConnection
        {
            get { return mMaintainConnection; }
            set { mMaintainConnection = value; }
        }

        /// <summary>
        /// Default constructor.  Creates a new DataAccess class with a new SqlConnection.
        /// </summary>
        public DataAccessBase()
        {
            if (String.IsNullOrEmpty(mConnectionString))
            {
                throw new ArgumentException("DataAccessBase.ConnectionString has not been set.");
            }

            mConnection = new SqlConnection(mConnectionString);
        }

        /// <summary>
        /// Overloaded constructor.  Creates a new DataAccess class with an existing
        /// SqlConnection.
        /// </summary>
        /// <param name="existingConnection">A valid SqlConnection.</param>
        public DataAccessBase(SqlConnection existingConnection)
        {
            mConnection = existingConnection;
        }

        /// <summary>
        /// Returns the first DataRow from the first DataTable in a Get() dataset.
        /// If no data was returned by the Get() method, a null will be returned.
        /// </summary>
        /// <param name="id">The primary key Id of the row to retrieve.</param>
        /// <returns>A single DataRow if data was returned, or null of no record was returned.</returns>
        public virtual DataRow GetRow(int id)
        {
            DataRow rtnVal = null;

            DataSet srcDataSet = Get(id);
            DataTable srcDataTable = null;

            Debug.Assert(srcDataSet.Tables.Count == 1);

            srcDataTable = srcDataSet.Tables[0];

            if (srcDataTable.Rows.Count == 1)
            {
                rtnVal = srcDataTable.Rows[0];
            }

            return rtnVal;
        }

        /// <summary>
        /// Returns a DataSet representing a single row of data, based on the 
        /// supplied Id.
        /// </summary>
        /// <param name="id">The primary key Id of the row to retrieve.</param>
        /// <returns>A DataSet containing the requested data.</returns>
        public virtual DataSet Get(int id)
        {
            throw new NotSupportedException("This method is not supported.");
        }

        /// <summary>
        /// Returns an empty DataTable initialized with the appropriate columns.
        /// </summary>
        /// <returns>A new DataTable.</returns>
        public virtual DataTable EmptyTable()
        {
            throw new NotSupportedException("This method is not supported.");
        }
    }
}
